<?php
namespace Face\Controller;
use Org\Util\DataTree;
class IndexController extends CommonController {

    public function index(){
        $count      = M('add')->where(['check'=>1])->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme'," %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('add')->where(['check'=>1])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }
        $this->assign(['page'=>$show,'data'=>$list]);
        $this->assign('lun',M('slide')->select());
        $this->display();
    }
    /*查找*/
    public function ser()
    {
        $ser = i('post.ser');
        $where= array('like','%'.$ser.'%');
        if(IS_POST){
            if(trim(empty($ser))){
                $this->error('查询内容不能为空');
            }
            $count      = M('add')->where(['title|cityname'=>$where,'check'=>1])->count();
            if($count==0){
                $this->error('没有找到你要找的内容,抱歉了。');
            }
            $Page       = new \Think\Page($count,10);
            $Page->setConfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
            $show       = $Page->show();
            $list = M('add')->where(['title|cityname'=>$where,'check'=>1])->order('sort desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            foreach ($list as $k=>$v) {
                $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
            }

            $this->assign(['page'=>$show,'data'=>$list]);
            $this->display();
        }else{
            $this->display();
        }
    }
    /*详细内容*/
    public function content()
    {

        $this->assign('web',M('web')->find(1));
        $id = i('get.id');
        $data = M('add')->where(['id'=>$id])->find();
        $data['city'] = M('city')->where(['id'=>$data['city']])->find();
        $read = $data['read']+1;
        $data1 = [
            'read'=>$read,
        ];
        $where  = ['neq',$id];
        M('add')->where(['id'=>$id])->save($data1);
        $data2 = M('add')->where(['id' =>$where])->order('id desc')->limit(10)->select();
        foreach ($data2 as $k=>$v) {
            $data2[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }
        $this->assign('other',$data2);
        $data3 = M('city')->where(['pid'=>0])->select();
        foreach ($data3 as $k=>$v){
            $data3[$k]['des'] = M('city')->where(['pid' => $v['id']])->select();
        }
        $this->assign('city',$data3);
        $this->assign('ad',$data);
        $this->display();
    }
    /*列表*/
    public function other()
    {
        $id = i('get.id');
        $count      = M('add')->where(['city'=>$id,'check'=>1])->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme'," %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('add')->where(['city'=>$id,'check'=>1])->order('sort desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }
        $this->assign(['page'=>$show,'data'=>$list]);

        $this->display();
    }
    /*发布广告*/
    public function send()
    {
        if(!isset($_SESSION['user'])){
            $this->redirect('user/login/index');
        }
        $tree = new DataTree();
        $data = $tree->tree(M('city')->select(),'name','id');
        $this->assign('cityi',$data);
        if(IS_POST){
            $post  = i('post.');
            $cityname = M('city')->where(['id'=>$post['type']])->find();
            $data = [
                'title'=>$post['title'],
                'key'=>$post['key'],
                'des'=>$post['des'],
                'time'=>date("Y-m-d H:i:s", time()),
                'content'=>$post['content'],
                'thumb'=>$post['thumb'],
                'check'=>0,
                'read'=>55,
                'city'=>$post['type'],
                'sort'=>1,
                'maker'=>$_SESSION['user']['user_nicename'],
                'login'=>$_SESSION['user']['user_login'],
                'cityname'=>$cityname['name']
            ];
            if(trim(empty($post['thumb']))){
                $this->error('请上传封面图');
            }
            if(trim(empty($post['content']))){
                $this->error('请输入内容');
            }
            if(trim(empty($post['key']))){
                $this->error('请输入广告关键词');
            }
            if(trim(empty($post['des']))){
                $this->error('请输入广告摘要');
            }
            if(trim(empty($post['title']))){
                $this->error('请输入广告标题');
            }
            if(M('add')->add($data)){
                $this->success('添加成功,请等待工作人员审核。');
            }else{
                $this->error('失败，未知错误');
            }
        }else{
            $this->display();
        }
    }
    /*我的发布*/
    public function my()
    {
        if(!isset($_SESSION['user'])){
            $this->redirect('user/login/index');
        }
        $count      = M('add')->where(['login'=>$_SESSION['user']['user_login']])->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme'," %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('add')->where(['login'=>$_SESSION['user']['user_login']])->order('sort desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['city'] = M('city')->where(['id' => $v['city']])->field('name')->find();
        }
        $this->assign(['page'=>$show,'data'=>$list]);
        $this->assign('lun',M('slide')->select());
        $this->display();
    }
    /*我的发布删除*/
    public function dell()
    {
        if(!isset($_SESSION['user'])){
            $this->redirect('user/login/index');
        }
        $id = i('get.id');
        if(M('add')->where(['id'=>$id])->delete()){
            $this->success('删除成功');
        }else{
            $this->error('失败，未知错误');
        }
    }
    /*编辑*/
    public function editt()
    {
        if(!isset($_SESSION['user'])){
            $this->redirect('user/login/index');
        }
        $id = i('get.id');
        $old = M('add')->where(['id'=>$id])->find();

        $this->assign('old',$old);
        $this->assign('cc',M('city')->where(['id'=>$old['city']])->find());
        $tree = new DataTree();
        $data = $tree->tree(M('city')->select(),'name','id');
        $this->assign('cityi',$data);
        if(IS_POST){
            $post  = i('post.');
            $cityname = M('city')->where(['id'=>$post['type']])->find();
            $data = [
                'title'=>$post['title'],
                'key'=>$post['key'],
                'des'=>$post['des'],
                'content'=>$post['content'],
                'thumb'=>$post['thumb'],
                'city'=>$post['type'],
                'maker'=>$_SESSION['user']['user_nicename'],
                'login'=>$_SESSION['user']['user_login'],
                'cityname'=>$cityname['name']
            ];
            if(trim(empty($post['thumb']))){
                $this->error('请上传封面图');
            }
            if(trim(empty($post['content']))){
                $this->error('请输入内容');
            }
            if(trim(empty($post['key']))){
                $this->error('请输入广告关键词');
            }
            if(trim(empty($post['des']))){
                $this->error('请输入广告摘要');
            }
            if(trim(empty($post['title']))){
                $this->error('请输入广告标题');
            }
            if(M('add')->where(['id'=>$id])->save($data)){
                $this->success('编辑修改成功');
            }else{
                $this->error('失败，未知错误');
            }
        }else{
            $this->display();
        }
    }
    /*服务协议*/
    public function reful()
    {
        $this->display();
    }
    /*公司简介*/
    public function com()
    {
        $this->display();
    }

}